//
//  MineAddressgUCAmcJLCFListViewControllernRoqhZ.m
//  qcb_ios
//
//  Created by Ji on 2018/9/3.
//  Copyright © 2018年 ski. All rights reserved.
//

#import "empwxzJLCFMineAddressListViewControllerTeFNdn.h"
#import "sHkPtEJLCFMineRequestozAYZs.h"
#import "VdoxvTJLCFMineAddressDataModelRVwObI.h"
#import "dBCpblJLCFMineAddressAddViewControllerjrpfOh.h"
#import "VFZOOBJLCFMineAddressListCellbCUwJx.h"

@interface MineAddressgUCAmcJLCFListViewControllernRoqhZ ()
{
    NSInteger _pageNumber;
}

@property (nonatomic, strong) NSMutableArray *aHafpVaddressListArrPzqwVJ;

@end

@implementation MineAddressgUCAmcJLCFListViewControllernRoqhZ

- (NSMutableArray *)aHafpVaddressListArrPzqwVJ
{
    if (!_aHafpVaddressListArrPzqwVJ) {
        _aHafpVaddressListArrPzqwVJ = [NSMutableArray array];
    }
    return _aHafpVaddressListArrPzqwVJ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addNavigationBar];
    self.navigationBar.title = @"收货地址管理";
    
    self.view.backgroundColor = QTZ_Gray237;
    self.tableView.frame = CGRectMake(0, NAVBAR_HEIGHT, FULL_WIDTH, self.view.height - NAVBAR_HEIGHT - 50);
    
    [self loadData];
    
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(20, FULL_HEIGHT - 50 - (IPHONEX ? 30 : 0), FULL_WIDTH - 40, 40)];
    [addBtn setBackgroundColor:Theme_Color];
    addBtn.titleLabel.font = FONT(15);
    [addBtn setTitle:@"添加地址" forState:0];
    addBtn.layer.cornerRadius = 5;
    [addBtn addTarget:self action:@selector(handleAdd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    
}

- (void)handleAdd
{
    
    dBCpblJLCFMineAddressAddViewControllerjrpfOh *vc = [dBCpblJLCFMineAddressAddViewControllerjrpfOh new];
    vc.complete = ^{
        [self loadData];
    };
    [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
}

- (void)loadData
{
    [self.aHafpVaddressListArrPzqwVJ removeAllObjects];
    _pageNumber = 1;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:@(_pageNumber) forKey:@"page"];
    
    [sHkPtEJLCFMineRequestozAYZs mineAddressListWith:params loadingInView:self.view success:^(WNbFQgJLCFMineAddressResultModelcDVvLd *address) {
        [self hideNoMoreDataNotice];
        if (address.data.count > 0) {
            [self.aHafpVaddressListArrPzqwVJ addObjectsFromSafeArray:address.data];
        } else {
            [self showNoMoreDataNotice:@"您暂时还没有填写过地址"];
        }
        [self.tableView reloadData];
        [self endRefresh];
        
    } failure:^(NSString *error) {
        [self endRefresh];
        [self showNotice:error];
    }];
    
}

- (void)loadMoreData
{
    _pageNumber ++;
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setSafeObject:@(_pageNumber) forKey:@"page"];
    
    [sHkPtEJLCFMineRequestozAYZs mineAddressListWith:params loadingInView:self.view success:^(WNbFQgJLCFMineAddressResultModelcDVvLd *address) {
        if (address.data.count > 0) {
            [self.aHafpVaddressListArrPzqwVJ addObjectsFromSafeArray:address.data];
        } else {
            self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
        [self endLoadMore];
        
    } failure:^(NSString *error) {
        [self endLoadMore];
        [self showNotice:error];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.aHafpVaddressListArrPzqwVJ.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VFZOOBJLCFMineAddressListCellbCUwJx *cell = [VFZOOBJLCFMineAddressListCellbCUwJx dequeueReusableCellForTableView:tableView];
    cell.address = [self.aHafpVaddressListArrPzqwVJ safeObjectAtIndex:indexPath.section];
    [cell reloadData];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VdoxvTJLCFMineAddressDataModelRVwObI *address = [self.aHafpVaddressListArrPzqwVJ safeObjectAtIndex:indexPath.section];;
    dBCpblJLCFMineAddressAddViewControllerjrpfOh *vc = [dBCpblJLCFMineAddressAddViewControllerjrpfOh new];
    vc.update = YES;
    vc.address = address;
    vc.complete = ^{
        [self loadData];
    };
    [[[JinLiCaiFuApplicationEntrace shareEntrance] currentNavController] pushViewController:vc animated:YES];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *rRHNlxheaderViewfJQeRl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 15)];
    rRHNlxheaderViewfJQeRl.backgroundColor = [UIColor clearColor];
    return rRHNlxheaderViewfJQeRl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VdoxvTJLCFMineAddressDataModelRVwObI *address = [self.aHafpVaddressListArrPzqwVJ safeObjectAtIndex:indexPath.section];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setSafeObject:address.id forKey:@"addressId"];
    [sHkPtEJLCFMineRequestozAYZs deleteAddressWith:dic loadingInView:self.view success:^{
        // 删除模型
        [self.aHafpVaddressListArrPzqwVJ removeObjectAtIndex:indexPath.section];
        
        // 刷新
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView reloadData];
    } failure:^(NSString *error) {
        [self showNotice:error];
    }];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([cell respondsToSelector:@selector(tintColor)]) {
        
        if (tableView == self.tableView) {
            
            CGFloat cornerRadius = 10.f;
            
            cell.backgroundColor = UIColor.clearColor;
            
            CAShapeLayer *layer = [[CAShapeLayer alloc] init];
            
            CGMutablePathRef pathRef = CGPathCreateMutable();
            
            CGRect bounds = CGRectInset(cell.bounds, 10, 0);
            
            if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                
                CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
                
            } else if (indexPath.row == 0) {
                
                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                
                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
                
            } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
                
                CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
                
                CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
                
                CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
                
            } else {
                
                CGPathAddRect(pathRef, nil, bounds);
                
            }
            
            layer.path = pathRef;
            
            CFRelease(pathRef);
            
            layer.fillColor = [UIColor colorWithWhite:1.f alpha:1.f].CGColor;
            
            
            UIView *testView = [[UIView alloc] initWithFrame:bounds];
            
            [testView.layer insertSublayer:layer atIndex:0];
            
            testView.backgroundColor = UIColor.clearColor;
            
            cell.backgroundView = testView;
            
        }
        
    }
    
}

@end
